import fetch from "../../utils/fetch";
const globalData = getApp().globalData;
const {
  gotoLogin
} = require('../../utils/util');
Page({

  
  /**
   * 页面的初始数据
   */
  data: {
    userId:0,  //用户id
    currentId:0,  //当前用户id
    info:{}, // 用户信息
    dynamicArr:[], // 动态数组
    dynamicCount:0,
    isConcern: false, // 是否关注
    dynamicPage:1,
     //长按删除显示
     showDeleteModel:false,
     pageX:0,    //x轴位置
     pageY:0,    //y轴位置
     postId: 0,   //记录待删除文章的序号
     index: 0,    //待删除文章的下标
     postIsFinish: false
  },
  size:3,  //每次获取十条文章
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.size = 3;
    this.data.userId = options.userId;
    this.data.currentId = globalData.userInfo.id?globalData.userInfo.id:'';
    //获取用户信息
    this.getUserInfo();
  },
  getUserInfo(){
    fetch('/users/center',{
      userId:this.data.userId,
      currentId:this.data.currentId,
      page:1,
      size:this.size
    }).then(res=>{
      const {info,dynamicArr,dynamicCount,isConcern} = res.data;
      let postIsFinish = this.data.postIsFinish;
      if(dynamicArr.length == dynamicCount){
        postIsFinish = true;
      }
      this.setData({
        currentId:this.data.currentId,
        info,
        dynamicArr,
        dynamicCount,
        isConcern,
        postIsFinish
      })
    })
  },
  concern(){
    if(!globalData.isLogin){
      gotoLogin();
      return;
    }
    const isConcern = this.data.isConcern;
    if(isConcern){
      wx.showModal({
        title: '关注',
        content: '确定取消关注吗',
        complete: (res) => {
          if (res.confirm) {
            fetch('/users/unfollow',{
              noticerId:globalData.userInfo.id,
              followerId:this.data.info.id
            }).then(res=>{
              this.setData({
                isConcern:!this.data.isConcern
              });
              wx.showToast({
                title: '取消关注',
                icon:'error',
              })
            }).catch(err=>{
              this.concern();
            })
          }
        }
      })
    }else{
      fetch('/users/follow',{
        noticerId:globalData.userInfo.id,
        followerId:this.data.info.id
      }).then(res=>{
        this.setData({
          isConcern:!this.data.isConcern
        });
        wx.showToast({
          title: '关注成功',
          icon:'success'
        })
      }).catch(err=>{
          this.concern();
      })
    }
  },
  //转向文章详情
  gotoDetail(e){
    const postId = e.currentTarget.dataset.postId;
    wx.navigateTo({
      url: '../postDetail/postDetail?postId=' + postId,
    });
  },
  getMorePost(){
    if(this.data.dynamicPage * this.size > this.data.dynamicCount){
      return ;
    }
    fetch('/post/dynamicList',{
      userId:this.data.info.id,
      page:++this.data.dynamicPage,
      size:this.size
    }).then(res=>{
      let dynamicArr = this.data.dynamicArr;
      if(dynamicArr.length + res.data.dynamicArr.length == this.data.dynamicCount){
        this.setData({
          postIsFinish: true
        })
      }
      res.data.dynamicArr.forEach(item=>{
        dynamicArr.push(item);
      });
      this.setData({
        dynamicArr,
      })
    }).catch(err=>{
      this.getMorePost();
    })
  },
  deleteDynamic(e){
    if(this.data.info.id != this.data.currentId){
      return;
    }
    this.data.postId = e.currentTarget.dataset.postId;
    this.data.index = e.currentTarget.dataset.index;
    const sys =  wx.getSystemInfoSync();
    const screenWidth = sys.screenWidth;
    let pageX = e.touches[0].pageX;
    let pageY = e.touches[0].pageY;
    if((screenWidth - pageX) < 150){
      pageX = pageX - 150;
    }
    this.setData({
      showDeleteModel:1,
      pageX:pageX,
      pageY:pageY
    })
  },

  //点击遮罩层隐藏删除窗口
  hideDeleteModel:function(){
    this.setData({
      showDeleteModel:0,
      pageY:0,
      pageX:0
    })
  },
  delete(){
    const postId = this.data.postId;
    const index = this.data.index;
    const navItem = this.data.navItem;
    this.setData({
      seleteDataArr:this.data.seleteDataArr,
      showDeleteModel:0,
      pageY:0,
      pageX:0
    })
    wx.showLoading({
      title: '文章删除中',
    });
    fetch('/post/delete',{
      postId: postId,
      userId:globalData.userInfo.id,
      postType:navItem+1
    }).then(res=>{
      if(navItem==0){
        globalData.userInfo.score -=1;
        wx.setStorageSync('userInfo', globalData.userInfo)
      }
      wx.showToast({
        title: '删除成功',
      });
      this.data.seleteDataArr.splice(index,1);
      this.setData({
        seleteDataArr:this.data.seleteDataArr,
        showDeleteModel:0,
        pageY:0,
        pageX:0
      })
    }).catch(err=>{
      console.log(err);
      this.delete();
    })
  },
  //前往关注或粉丝列表或者积分详情
  gotoList(e){
    const itemId = e.currentTarget.dataset.itemId;
    const currentId = globalData.userInfo.id?globalData.userInfo.id:'';
    const userId = this.data.userId;
    if(userId != currentId)
      return ;
    if(itemId == 3){
      wx.navigateTo({
        url: '../scoreRecord/scoreRecord',
      })
    } else{
      wx.navigateTo({
        url: '../concernFanList/concernFanList?type=' + itemId,
      })
    }
  },

})